package org.jeecg.modules.order;

import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.modules.order.entity.OrderInfo;
import org.jeecg.common.modules.order.service.IOrderInfoService;
import org.jeecg.common.modules.order.vo.OrderInfoReqVo;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.modules.wx.service.IWxUserinfoService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.vo.OrderInfoVo;
import org.jeecg.modules.system.vo.ResultVo;
import org.jeecg.utils.ExportUtil;

import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: order
 * @Author: jeecg-boot
 * @Date:   2020-08-26
 * @Version: V1.0
 */
@Api(tags="order")
@RestController
@RequestMapping("/order/order")
@Slf4j
public class OrderController extends JeecgController<OrderInfo, IOrderInfoService> {
	@Autowired
	private IOrderInfoService orderService;

	@Autowired
	private IWxUserinfoService wxUserinfoService;

	@Autowired
	private IDoctorService doctorService;

	/**
	 * 分页列表查询
	 *
	 * @param orderInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "order-分页列表查询")
	@ApiOperation(value="order-分页列表查询", notes="order-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(OrderInfo orderInfo,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
		//数据开始的记录数
		int dataStart = (pageNo - 1) * pageSize;
		Integer status;
		//先查询所有的患者、用户信息
		String id     = req.getParameter("id");
		String sstatus = req.getParameter("status");
		if(sstatus != null){
			status = Integer.valueOf(sstatus);
		}else{
			status = -1;
		}
		String userName = req.getParameter("userName");
		String doctorName = req.getParameter("doctorName");

		//获取查询的数据
		List<OrderInfoReqVo> orderInfoReqVos = orderService.getOrderPageInfos(id,status,userName,doctorName,dataStart,pageSize);

		//查询到的总数据数
		Integer count = orderService.getCount(id,status,userName,doctorName);

		//组合分页展示的数据
		ResultVo vo = this.initResultVo(orderInfoReqVos,count,pageNo,pageSize);

		return Result.ok(vo);
	}

	 /**
	  * 组合返回的信息
	  * @param orderInfoReqVos
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 private ResultVo initResultVo(List orderInfoReqVos, Integer count,Integer pageNo, Integer pageSize){
		 ResultVo vo = new ResultVo();
		 double dCount    = Double.valueOf(count);       //查询的数据总条数
		 double dPageSize = Double.valueOf(pageSize);    //每页显示的条数
		 int pages = (int) Math.ceil(dCount / dPageSize);//页码数量

		 vo.setCurrent(pageNo);
		 vo.setHitCount(false);
		 vo.setOptimizeCountSql(true);
		 vo.setOrders(new ArrayList<>());
		 vo.setRecords(orderInfoReqVos);
		 vo.setPages(pages);
		 vo.setSearchCount(true);
		 vo.setSize(pageSize);
		 vo.setTotal(count);
		 return vo;
	 }

	/**
	 *   添加
	 *
	 * @param orderInfo
	 * @return
	 */
	@AutoLog(value = "order-添加")
	@ApiOperation(value="order-添加", notes="order-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody OrderInfo orderInfo) {
		orderService.save(orderInfo);
		return Result.ok("添加成功！");
	}
	/**
	 *   添加
	 *
	 * @param orderInfo
	 * @return
	 */
	@AutoLog(value = "order-退款")
	@ApiOperation(value="order-退款", notes="order-退款")
	@PostMapping(value = "/doRefundNew")
	public Result<?> doRefundNew(@RequestBody OrderInfo orderInfo) {
		orderService.save(orderInfo);
		return Result.ok("退款成功！");
	}
	/**
	 *  编辑
	 *
	 * @param orderInfo
	 * @return
	 */
	@AutoLog(value = "order-编辑")
	@ApiOperation(value="order-编辑", notes="order-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody OrderInfo orderInfo) {
		orderService.updateById(orderInfo);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "order-通过id删除")
	@ApiOperation(value="order-通过id删除", notes="order-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		orderService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "order-批量删除")
	@ApiOperation(value="order-批量删除", notes="order-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.orderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "order-通过id查询")
	@ApiOperation(value="order-通过id查询", notes="order-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		//查询订单信息
		OrderInfo orderInfo = orderService.getById(id);
		if(orderInfo ==null) {
			return Result.error("未找到对应数据");
		}

		//查询订单对应的用户信息
		WxUserinfo wxUserinfo = wxUserinfoService.getById(orderInfo.getWxUserinfoId());

		//查询订单对应的医生信息
		Doctor doctorInfo = doctorService.getById(orderInfo.getDoctorId());

		//组合订单和用户信息
		OrderInfoVo vo = this.initOrderInfoVo(wxUserinfo,doctorInfo,orderInfo);
		List<OrderInfoVo> arr = new ArrayList();
		arr.add(vo);
		return Result.ok(arr);
	}

	 /**
	  * 组合订单、用户数据
	  * @param wxUserinfo
	  * @param orderInfo
	  * @return
	  */
	 private OrderInfoVo initOrderInfoVo(WxUserinfo wxUserinfo ,Doctor doctorInfo ,OrderInfo orderInfo){
		 OrderInfoVo vo = new OrderInfoVo();
		 vo.setId(orderInfo.getId());
		 vo.setCreateTime(orderInfo.getCreateTime());
		 vo.setShopType(orderInfo.getShopType());
		 vo.setStatus(orderInfo.getStatus());
		 vo.setUserName(wxUserinfo.getNickname());
		 vo.setDoctorName(doctorInfo.getName());
		 vo.setAmount(orderInfo.getAmount());
		 return vo;
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param orderInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OrderInfo orderInfo) {
        return super.exportXls(request, orderInfo, OrderInfo.class, "order");
    }

//		List<OrderInfo> orders = orderService.list();
//		return ExportUtil.exportXls(orders,OrderInfo.class,"测试导出");


    @RequestMapping(value = "exportXlsTest")
    public ModelAndView exportXls(HttpServletRequest request){
    	//【一】第一种方式
		List<OrderInfoReqVo> list;
		//获取订单和用户表的关联信息
		String selections = request.getParameter("selections");  //选中的id串
		if (oConvertUtils.isNotEmpty(selections)) {
			List<String> selectionList = Arrays.asList(selections.split(",")); //id数组
			list = orderService.excelOrderInfo(selectionList);
		} else {
			List<String> arr = new ArrayList<>();
			list = orderService.excelOrderInfo(arr);
		}
//
//		log.info("\n\t++++++++++++++++++++++++++获取到的数据+++++++++++++++++++++++++++\n\t"+
//				list
//				+"+++++++++++++++++++++++++++++++++++++++++++++++++++++\n\t");
		return ExportUtil.exportXls(list,OrderInfoReqVo.class,"测试导出");
	}

    /**
	* 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OrderInfo.class);
    }
}
